﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppearanceDetectionStation
{
    public partial class Form9 : Form
    {
        public Form9()
        {
            InitializeComponent();
            Form16.timecyc.Stop();
        }
        public static System.Timers.Timer timecyc = new System.Timers.Timer();
        private void Form9_Load(object sender, EventArgs e)
        {
            timecyc.Enabled = true;
            timecyc.Interval = 1000;
            timecyc.AutoReset = true;
            timecyc.Elapsed += new System.Timers.ElapsedEventHandler(timecyc_Elapsed);
        }
        int readDone;
        void timecyc_Elapsed(object sender ,System.Timers.ElapsedEventArgs e)
        {
            if (readDone==0)
            {
                List<PictureBox> PB = new List<PictureBox> { pictureBox1, pictureBox2, pictureBox3, pictureBox4, pictureBox5, pictureBox6, pictureBox7, pictureBox8, pictureBox9 };

                int[] aryyFile = GetLatestFileNameInfo(@"d:\相机", ".jpeg");

                if (aryyFile.Length>0)
                {
                    Sort(ref aryyFile);
                    DirectoryInfo d = new DirectoryInfo(@"d:\相机");
                    for (int i = 0; i < aryyFile.Length; i++)
                    {
                        if (i > 8)
                        {
                            break;
                        }

                        FileStream fs = new FileStream(@"d:\相机\" + d.GetFiles(aryyFile[i] + ".jpeg")[0].ToString(), FileMode.Open, FileAccess.Read);
                        int byteLength = (int)fs.Length;
                        byte[] fileBytes = new byte[byteLength];
                        fs.Read(fileBytes, 0, byteLength);
                        fs.Close();
                        PB[i].Image = Image.FromStream(new MemoryStream(fileBytes));

                        if (i == 8)
                        {
                            CopyAllFile(@"d:\相机", @"d:\照片\123");

                            readDone = 1;
                        }
                    }  
                }
            }
            else if (readDone==1)
            {
                deletePhtor(@"d:\相机", ".jpeg");
                readDone = 0;
            }
        }
        //获取照片
        static int[] GetLatestFileNameInfo(string dir, string ext)
        {
            DirectoryInfo d = new DirectoryInfo(dir);
            int[] FileName=new int[d.GetFiles().Length-1];
            int a = 0;
            foreach (FileInfo file in d.GetFiles())
            {
                if (file.Extension.ToUpper() == ext.ToUpper())
                {
                    FileName[a] = Convert.ToInt32(System.IO.Path.GetFileNameWithoutExtension(file.FullName));
                    a++;
                }
            }

            return FileName;
        }
        /// <summary>
        /// 将文件夹下的所有文件复制到另外一个文件夹
        /// </summary>
        /// <param name="sources">源目录</param>
        /// <param name="dest">目标目录</param>
        public static void CopyAllFile(string sources, string dest)
        {
            if (!Directory.Exists(dest))
            {
                Directory.CreateDirectory(dest);
            }

            string[] files = Directory.GetFileSystemEntries(sources);
            foreach (var file in files)
            {
                string destFileName = Path.Combine(dest, Path.GetFileName(file));
                if (File.Exists(file))
                {
                    File.Copy(file, destFileName, true);
                }
                else
                {
                    if (!Directory.Exists(file))
                    {
                        Directory.CreateDirectory(file);
                    }
                    else
                    {
                        CopyAllFile(Path.Combine(sources, Path.GetFileName(file)), destFileName);
                    }
                }
            }
        }

        //删除照片
        static string deletePhtor(string dir, string ext)
        {
            DirectoryInfo d = new DirectoryInfo(dir);
            string FileName = "0";

            foreach (FileInfo file in d.GetFiles())
            {
                if (file.Extension.ToUpper() == ext.ToUpper())
                {
                    try
                    {
                        File.Delete(file.FullName);
                    }
                    catch
                    { }
                }
            }

            return FileName;
        }
        static void Sort(ref int[] arrNum)//传递一个数组排序
        {
            //冒泡排序
            for (int i = 0; i < arrNum.Length - 1; i++)
            {
                for (int j = 0; j < arrNum.Length - 1 - i; j++)
                {
                    if (arrNum[j] > arrNum[j + 1])
                    {
                        int temp = arrNum[j];
                        arrNum[j] = arrNum[j + 1];
                        arrNum[j + 1] = temp;
                    }
                }
            }
        }
    }
}
